﻿
Imports webctrl

Public Class trace
    Public Const functionName As String = "trc"
    Public Shared ReadOnly eventPing As String = """" + FDInternalRequest.ping + _
                                                 "?x=""+(e.x?e.x:e.pageX)+""" + _
                                                 "&y=""+(e.y?e.y:e.pageY)+""" + _
                                                 "&k=""+(e.which?e.which:e.keyCode)+""" + _
                                                 "&""+"

    Shared Sub New()
        assert(strstartwith(FDInternalRequest.ping, network.pathSeperator))
        assert(Not strcont(FDInternalRequest.ping, network.questionMark))
    End Sub

    Private Shared Function functionNameOverride(ByVal recordPrecision As UInt64) As String
        If recordPrecision > 1 Then
            Return functionName + Convert.ToString(recordPrecision)
        Else
            Return functionName
        End If
    End Function

    Private Shared Function timeRecord(ByVal recordPrecision As UInt64) As String
        assert(recordPrecision > 1)
        Return functionName + "t" + Convert.ToString(recordPrecision)
    End Function

    Public Shared Sub register(ByVal inlineScript As inlineScript, _
                               ByVal recordPrecision As UInt64)
        assert(Not inlineScript Is Nothing)
        Dim content As String = Nothing
        If recordPrecision > 1 Then
            Dim timeRecordStr As String = Nothing
            timeRecordStr = timeRecord(recordPrecision)
            var.invoke(inlineScript, timeRecordStr, 0)
            content = timeRecordStr + "++;" + _
                      ifelse.invoke(timeRecordStr + "%" + Convert.ToString(recordPrecision) + "==0", _
                                    invoke(inlineScript, 1, "e", "m"))
        Else
            content = backgroundDownload.invoke(inlineScript, functionName + "bd", _
                                                eventPing + traceIdVar.name(inlineScript) + "+""&""+m", Nothing)
        End If
        inlineScript.addscript(functionNameOverride(recordPrecision), _
                               "function " + functionNameOverride(recordPrecision) + "(e,m){" + content + "}")
    End Sub

    Public Shared Function invoke(ByVal inlineScript As inlineScript, _
                                  ByVal recordPrecision As UInt64, _
                                  Optional ByVal eventVar As String = "event", _
                                  Optional ByVal eventMark As String = Nothing, _
                                  Optional ByRef actionTarget As String = Nothing) As String
        If Not inlineScript Is Nothing Then
            register(inlineScript, recordPrecision)
        End If
        actionTarget += functionNameOverride(recordPrecision) + "(" + _
                            eventVar + "," + _
                            ifoper(isemptystring(eventMark), null.name(inlineScript), eventMark) + ")"
        Return actionTarget
    End Function
End Class
